package test.jusfy.com.myapplication;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import win.smartown.android.library.tableLayout.TableAdapter;
import win.smartown.android.library.tableLayout.TableLayout;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link GridFragment} interface
 * to handle interaction events.
 * Use the {@link GridFragment} factory method to
 * create an instance of this fragment.
 */
public class GridFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    View view;
    LinearLayout titleLayout;//标题
    HorizontalScrollView horizontalListView;
    ListView listView;
    SimpleCursorAdapter adapter;
    private Cursor cursor;

    TableLayout tableLayout;
    List<SongTable> contentList;

    public GridFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_grid, container, false);

        tableLayout = view.findViewById(R.id.main_table);
        /*titleLayout = view.findViewById(R.id.titleLayoutId);
        horizontalListView = view.findViewById(R.id.horizontal_scrollView);
        listView = view.findViewById(R.id.optional_horizontal_listView);*/
        //构建线性布局参数对象
        /*LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.rightMargin = 10;
        params.gravity = Gravity.CENTER_HORIZONTAL;
        String[] titles = new String[]{"_id", "loc", "len", "mp3loc", "mp3len", "songname", "language", "singer", "search", "M", "L", "searchsinger", "midiname", "mp3name", "mtvname", "dbpath", "ensongname", "ensingername", "encomposer", "enwriter", "searchcomposer"};
        for(int i=0;i<titles.length;i++){
            TextView tv = new TextView(getActivity());
            tv.setText(titles[i]);
            tv.setTextSize(18);
            titleLayout.addView(tv);
            tv.setLayoutParams(params);
        }
        horizontalListView.smoothScrollTo(0, 0);*/
        initContent();
        firstRowAsTitle();
        //firstColumnAsTitle();
        return view;
    }

    SongTable songtable = null;

    private void initContent() {
        contentList = new ArrayList<>();
        String sql = "select id AS _id,loc,len,mp3loc,mp3len,songname,language,singer,search,M,L,searchsinger,midiname,mp3name,mtvname,dbpath,ensongname,ensingername,encomposer,enwriter,searchcomposer from songtable";
        cursor = MyApplication.query(sql, null);
        SongTable songtable1 = new SongTable("id", "loc", "len", "mp3loc", "mp3len", "songname", "language", "singer", "search", "M", "L", "searchsinger", "midiname", "mp3name", "mtvname", "dbpath", "ensongname", "ensingername", "encomposer", "enwriter", "searchcomposer");
        contentList.add(songtable1);
        if (cursor.moveToNext()) {
            do {
                songtable = new SongTable();
                songtable.setId(cursor.getString(cursor.getColumnIndex("_id")));
                songtable.setLoc(cursor.getString(cursor.getColumnIndex("loc")));
                songtable.setLen(cursor.getString(cursor.getColumnIndex("len")));
                songtable.setMp3loc(cursor.getString(cursor.getColumnIndex("mp3loc")));
                songtable.setMp3len(cursor.getString(cursor.getColumnIndex("mp3len")));
                songtable.setSongname(cursor.getString(cursor.getColumnIndex("songname")));
                songtable.setLanguage(cursor.getString(cursor.getColumnIndex("language")));
                songtable.setSinger(cursor.getString(cursor.getColumnIndex("singer")));
                songtable.setSearch(cursor.getString(cursor.getColumnIndex("search")));
                songtable.setM(cursor.getString(cursor.getColumnIndex("M")));
                songtable.setL(cursor.getString(cursor.getColumnIndex("L")));
                songtable.setSearchsinger(cursor.getString(cursor.getColumnIndex("searchsinger")));
                songtable.setMidiname(cursor.getString(cursor.getColumnIndex("midiname")));
                songtable.setMp3name(cursor.getString(cursor.getColumnIndex("mp3name")));
                songtable.setMtvname(cursor.getString(cursor.getColumnIndex("mtvname")));
                songtable.setDbpath(cursor.getString(cursor.getColumnIndex("dbpath")));
                songtable.setEnsongname(cursor.getString(cursor.getColumnIndex("ensongname")));
                songtable.setEnsingername(cursor.getString(cursor.getColumnIndex("ensingername")));
                songtable.setEncomposer(cursor.getString(cursor.getColumnIndex("encomposer")));
                songtable.setEnwriter(cursor.getString(cursor.getColumnIndex("enwriter")));
                songtable.setSearchcomposer(cursor.getString(cursor.getColumnIndex("searchcomposer")));
                contentList.add(songtable);
            }while (cursor.moveToNext());

            //cursor.moveToNext();
    }
        Log.d("contentList","initContent: "+contentList.toString());
}

    //将第一列作为标题
    private void firstColumnAsTitle() {
        tableLayout.setAdapter(new TableAdapter() {
            @Override
            public int getColumnCount() {
                return contentList.size();
            }

            @Override
            public String[] getColumnContent(int position) {
                return contentList.get(position).toArray();
            }
        });
    }

    //将第一行作为标题
    private void firstRowAsTitle() {
        //fields是表格中要显示的数据对应到Content类中的成员变量名，其定义顺序要与表格中显示的相同
        final String[] fields = {"id", "loc", "len", "mp3loc", "mp3len", "songname", "language", "singer", "search", "M", "L", "searchsinger", "midiname", "mp3name", "mtvname", "dbpath", "ensongname", "ensingername", "encomposer", "enwriter", "searchcomposer"};
        tableLayout.setAdapter(new TableAdapter() {
            @Override
            public int getColumnCount() {
                return fields.length;
            }

            @Override
            public String[] getColumnContent(int position) {
                //int rowCount = contentList.size();
                int rowCount = 100;
                String contents[] = new String[rowCount];
                try {
                    Field field = SongTable.class.getDeclaredField(fields[position]);
                    field.setAccessible(true);
                    for (int i = 0; i < rowCount; i++) {
                        contents[i] = (String) field.get(contentList.get(i));
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                return contents;
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if(cursor!=null){
            cursor.close();
        }
    }
}
